<?php
	require "." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "config.php";
	require "." . DIRECTORY_SEPARATOR . ".." . DIRECTORY_SEPARATOR . "dbUtil.php";
	
	$sqlErrorCode = "";
	$sqlErrorText = "";
	$sqlStmt = "";
	$configFile = ".". DIRECTORY_SEPARATOR ."config.txt";
		
	function createTables($con)
	{
		echo "Create Tables: ";
		
		$sqlFileToExecute = "." . DIRECTORY_SEPARATOR . "sql" . DIRECTORY_SEPARATOR . "createTables.sql";
		$file = fopen($sqlFileToExecute, "r+") or exit("Unable to open file!");
		$sqlFile = fread($file, filesize($sqlFileToExecute));
		fclose($file);
		$sqlArray = explode(';', $sqlFile);
		foreach ($sqlArray as $stmt) 
		{
			if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') 
			{
				$result = mysql_query($stmt);
				
				if (!$result) 
				{
					$sqlErrorCode = mysql_errno();
					$sqlErrorText = mysql_error();
					$sqlStmt = $stmt;
					break;
				}
			}
		}
		
		if (!isset($sqlErrorCode) || $sqlErrorCode == 0) 
		{
			echo " <span style=\"color: green;\">succesfully!</span><br />";
		} 
		else 
		{
			echo " <span style=\"color: red;\">Error!</span><br />";
			ende($con);
		}
	}
	
	function insertRechte($con)
	{
		echo "Insert Rechte: ";
	
		$sqlFileToExecute = "." . DIRECTORY_SEPARATOR . "sql" . DIRECTORY_SEPARATOR . "insertRechte.sql";
		$file = fopen($sqlFileToExecute, "r+") or exit("Unable to open file!");
		$sqlFile = fread($file, filesize($sqlFileToExecute));
		fclose($file);
		$sqlArray = explode(';', $sqlFile);
		foreach ($sqlArray as $stmt) 
		{
			if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') 
			{
				$result = mysql_query($stmt);
				
				if (!$result) 
				{
					$sqlErrorCode = mysql_errno();
					$sqlErrorText = mysql_error();
					$sqlStmt = $stmt;
					break;
				}
			}
		}
		
		if (!isset($sqlErrorCode) || $sqlErrorCode == 0) 
		{
			echo " <span style=\"color: green;\">succesfully!</span><br />";
		} 
		else 
		{
			echo " <span style=\"color: red;\">Error!</span><br />";
			ende($con);
		}
	}
	
	function insertTestdaten($con)
	{
		echo "Insert Testdaten: ";
	
		$sqlFileToExecute = "." . DIRECTORY_SEPARATOR . "sql" . DIRECTORY_SEPARATOR . "insertTestAccounts.sql";
		$file = fopen($sqlFileToExecute, "r+") or exit("Unable to open file!");
		$sqlFile = fread($file, filesize($sqlFileToExecute));
		fclose($file);
		$sqlArray = explode(';', $sqlFile);
		foreach ($sqlArray as $stmt) 
		{
			if (strlen($stmt)>3 && substr(ltrim($stmt),0,2)!='/*') 
			{
				$result = mysql_query($stmt);
				
				if (!$result) 
				{
					$sqlErrorCode = mysql_errno();
					$sqlErrorText = mysql_error();
					$sqlStmt = $stmt;
					break;
				}
			}
		}
		
		if (!isset($sqlErrorCode) || $sqlErrorCode == 0) 
		{
			echo " <span style=\"color: green;\">succesfully!</span><br />";
		} 
		else 
		{
			echo " <span style=\"color: red;\">Error!</span><br />";
			ende($con);
		}
	}
	
	function ende($con)
	{
		if (!isset($sqlErrorCode) || $sqlErrorCode == 0) 
		{
			mysql_query("COMMIT");
			echo "Script is executed succesfully!";
		} 
		else 
		{
			mysql_query("ROLLBACK");
			echo "An error occured during installation!<br/>";
			echo "Error code: $sqlErrorCode<br/>";
			echo "Error text: $sqlErrorText<br/>";
			echo "Statement:<br/> $sqlStmt<br/>";
			
		}
		
		DbUtil::CloseConnection($con);
		exit();
	}
	
	
	if(isset($_POST["dblocation"]) && 
	   isset($_POST["dbuser"]) &&
	   isset($_POST["dbpass"]))
    {
		$file = fopen($configFile, "w") or exit("Unable to open file!");
		
		fwrite($file, 'dblocation:'.$_POST["dblocation"].PHP_EOL);
		fwrite($file, 'dbuser:'.$_POST["dbuser"].PHP_EOL);
		fwrite($file, 'dbpass:'.$_POST["dbpass"].PHP_EOL);
		
		fclose($file);	
		
		Config::resetConfig($configFile );
		
		$con = DbUtil::GetConnectionWithoutDatabase();
		
		mysql_query("SET AUTOCOMMIT=0");
		mysql_query("START TRANSACTION");
		
		$result = null;
		$stmt = null;
		
		if(isset($_POST["Database"]) && $_POST["Database"] == "Database")
		{
			$result = mysql_query("create database leos") or die("Fehler beim erstellen der Datenbank");
		
		
			if (!$result) 
			{
				$sqlErrorCode = mysql_errno();
				$sqlErrorText = mysql_error();
				$sqlStmt = $stmt;
			}
		}
		else
		{		
			mysql_select_db(Config::$MYSQL_DATENBANK)
				or die ("Die Datenbank existiert nicht.");
			
			
			if(isset($_POST["Tables"]) && $_POST["Tables"] == "Tables")
			{
				createTables($con);
			}
			
			if(isset($_POST["Rechte"]) && $_POST["Rechte"] == "Rechte")
			{
				insertRechte($con);
			}
			
			if(isset($_POST["Testdaten"]) && $_POST["Testdaten"] == "Testdaten")
			{
				insertTestdaten($con);
			}
		}
		
		ende($con);
	}
	else
	{
		echo
<<<'EOT'
	<html>
		<head>
			<title>DB Install</title>
		</head>
		<body>
			<form action="install.php" method="post">
				<table  width="100%">
					<tr>
						<td>
							<table align="center" style="background: #bfbbbb;">
								<tr>
									<td>Location:</td>
									<td><input type="text" name="dblocation"></td>
								</tr>
								<tr>
									<td>User:</td>
									<td><input type="text" name="dbuser"></td>
								</tr>
								<tr>
									<td>Passwort:</td>
									<td><input type="text" name="dbpass"></td>
								</tr>
								<tr>
									<td colspan=2><input type="checkbox" name="Database" value="Database"> Create Database</td>
								</tr>
								<tr>
									<td colspan=2><input type="checkbox" name="Tables" value="Tables"> Create Tables</td>
								</tr>
								<tr>
									<td colspan=2><input type="checkbox" name="Rechte" value="Rechte"> Insert Rechte</td>
								</tr>
								<tr>
									<td colspan=2><input type="checkbox" name="Testdaten" value="Testdaten"> Insert Testdaten</td>
								</tr>
								<tr>
									<td colspan=2><input type="submit"></td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</form>
		</body>
	</html>
EOT;
	}
?>